﻿using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class MainForm
    {
        private void sheetCalendar_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (CurrentRota == null)
            {
                return;
            }
            /* A word of explanation is needed. We retrieve the DataGridView from sender
             * and we access the Cell in question directly. 
             * MSDN advices against it -
             * "to avoid performance penalties When handling this event,
             * access the Cell through the parameters of the event handler
             * rather than accessing the Cell directly."
             * 
             * The problem is that we don't want to set the same colour to the same Cell
             * over and over again.
             * And candidate.CellStyle always "forgets" what colour had the Cell had! */
            var dgrid = sender as DataGridView;
            var _col = e.ColumnIndex; // just to be concise
            var _row = e.RowIndex; // just to be concise
            var _highlight = (((Rota) sheetCalendar.Rows[e.RowIndex].DataBoundItem).Monday ==
                              CurrentRota.Monday);
            var color = _highlight ?
                                       Settings.Default.DGRIDCalendar_CurrentlyEditedBackColor :
                                                                                                   SystemColors.Window;
            if (dgrid[_col, _row].Style.BackColor != color)
                dgrid[_col, _row].Style.BackColor = color;
            
        }

        private void cbHideArchiveOnes_CheckedChanged(object sender, EventArgs e)
        {
            if (cbHideArchiveOnes.Checked)
            {
                sheetCalendar.DataSource = Program.CurrentFile.RotasExcludingPastDataSource;
            }
            else
            {
                sheetCalendar.DataSource = Program.CurrentFile.RotasDataSource;
            }
        }

        private void sheetCalendar_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            #region column "Action" (button column)
            if (e.ColumnIndex == 3)
            {
                var _selected = RotaSelectedInCalendar;
                PerformAction(_selected, _selected.ProperAction);
            }
            #endregion
        }
    }
}
